<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
	xmlns:fb="http://www.facebook.com/2008/fbml">
<div class="modal hide fade" id="simpleLoginModal">	
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal">×</button>
		<h3 id="myModalLabel">
			<g:message code="auth.login.title" />
		</h3>
	</div>
	<div class="modal-body">

		<g:isOnline>
		<form method='POST' id="ajaxFacebookLoginForm">
			<h5 class="fl">
				<g:message code="auth.login.with.facebook" />
				&nbsp;&nbsp;
			</h5>
			<fb:login-button class="facebookLoginButton"
				scope="email,publish_stream" onlogin="onFacebookLogin();">
				<g:message code="auth.login.facebook" />
			</fb:login-button>

			<div class="cleaner"></div>
			
			<g:render template="/common/facebookLogoutIfNotCurrentUser"/>
			
			<input id="accessToken" name="j_facebook_token" type="hidden">
			<div id="fb-root"></div>
		</form>
		
		<form action="${request.contextPath}/${grailsApplication.config.facebook.security.filter.url}"
			method='POST' id="hiddenFacebookLoginForm">
			<input id="accessToken2" name="j_facebook_token" type="hidden">
		</form>
		
		<div class="cleaner"></div>
		
		<hr></hr>
		
		</g:isOnline>
		

		

		<g:render template="/login/siteLogin" />
	</div>

	<div class="cleaner"></div>

	<div class="modal-footer">
		<g:message code="auth.login.new.user" />
		<a data-toggle="modal" href="#siteSigninModal" class="btn"
			data-dismiss="modal"><g:message code="auth.login.new.user.now" /></a>
	</div>
</div>

<div class="modal hide fade" id="facebookSigninModal">
	<g:render template="/login/facebookSignin" />
</div>

<div class="modal hide fade" id="siteSigninModal">
	<g:render template="/login/siteSignin" />
</div>

<g:isOnline>
<script type="text/javascript"
		xmlns:fb="http://www.facebook.com/2008/fbml">
	
	jQuery(document).ready(function() {
				
		function accessTokenUpdate(response) {
			if (response.authResponse)
				$('#accessToken').attr({value : response.authResponse.accessToken});
			else
				$('#accessToken').attr({value : ''});
		}
		   
		FB.getLoginStatus(accessTokenUpdate);
		   
		FB.Event.subscribe('auth.authResponseChange', accessTokenUpdate);
		FB.Event.subscribe('auth.login', accessTokenUpdate);
		
		FB.Event.subscribe('auth.logout', function(response) {
	   	FB.getLoginStatus(accessTokenUpdate);  
	  });
	  
	  if ('${showLogin}')
		$('#simpleLoginModal').modal('show');
  	});

  function onFacebookLogin() {
	  
	  
   	jQuery.ajax({
                 type: 'POST',
                 data: $('#ajaxFacebookLoginForm').serialize(),        
                 url: '${request.contextPath}/${grailsApplication.config.facebook.security.filter.url}',
                 success: function(data,textStatus) { 
                	 
                 		  if(data.success){
                              javascript: scroll(0, 0); 
                              window.location.href = '${createLink(uri: '/')}';
                           } 
                           else if (data.error) {
                        	  FB.api('/me', function(response) {
			                    $('#inputUserNameFacebook').attr({value : response.username});
					            $('#inputEmailFacebook').attr({value : response.email});
					            $('#inputNameFacebook').attr({value : response.first_name});
					            $('#inputLastnameFacebook').attr({value : response.last_name});
					              
					            $('#simpleLoginModal').modal('hide');
			 							$('#facebookSigninModal').modal('show');       
					          });  
                           }
                           else {
                           	 window.location.href = '${redirectUrl}'
                           }
                 }
  			});
  }
</script>
</g:isOnline>

</html>